<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>示例</title>

    <!-- Bootstrap -->
    <link href="plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<div class="container" style="margin-top: 15px">
    <div id="base"></div>
    <div id="qart"></div>

    <div class="col-md-6 ">
        <form class="">
            <div class="form-group">
                <label for="value">请输入文本或网址</label>
                <input type="text" class="form-control" id="value" value="www.baidu.com" placeholder="请输入文本或网址" />
            </div>

            <div class="form-group">
                <label for="file">请选择图片</label>
                <button type="button" class="btn btn-primary choose-file" style="display: inline-block;">选择图片</button>

                <input type="file" id="file" style="display: none">
            </div>

        </form>
        <div class="bs-example bs-example-tabs" data-example-id="togglable-tabs">
            <ul id="myTabs" class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active"><a href="#home" id="home-tab" role="tab" data-toggle="tab" aria-controls="home" aria-expanded="false">markQrCode</a></li>
                <li role="presentation" class="dropdown">
                    <a href="#" id="myTabDrop1" class="dropdown-toggle" data-toggle="dropdown" aria-controls="myTabDrop1-contents">makeQArt <span class="caret"></span></a>
                    <ul class="dropdown-menu" aria-labelledby="myTabDrop1" id="myTabDrop1-contents">
                        <li><a href="#threshold" role="tab" id="threshold-tab" data-toggle="tab" aria-controls="threshold">threshold</a></li>
                        <li><a href="#color" role="tab" id="color-tab" data-toggle="tab" aria-controls="color">color</a></li>
                    </ul>
                </li>
            </ul>
            <div id="myTabContent" class="tab-content">
                <div role="tabpanel" class="tab-pane fade active in" id="home" aria-labelledby="home-tab">
                    <p></p>
                </div>

                <div role="tabpanel" class="tab-pane fade" id="threshold" aria-labelledby="threshold-tab">
                    <p data-content="有文件时生成二维码"></p>
                </div>
                <div role="tabpanel" class="tab-pane fade" id="color" aria-labelledby="color-tab">
                    <p data-content="有文件时生成二维码"></p>

                </div>
            </div>
        </div>

    </div>

</div>


<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="plugins/bootstrap/js/bootstrap.min.js"></script>

<script src="qrcode.js"></script>
<script src="qart.min.js"></script>
<script type="text/javascript">
    /**
     * 生成普通二维码,返回图片文本值
     * @param option
     */
    function markQrCode(option) {
        var defaultOption = {
            typeNumber: 10,
            level: "H",  //'L','M','Q','H'
            value: null,
            cellSize: 3,
            margin: undefined
        };
        option = $.extend(defaultOption, option);
        var qr = qrcode.QRCode(option.typeNumber, option.level);
        qr.addData(option.value);
        qr.make();
        return qr.createImgTag(option.cellSize, option.margin);
    }


    /**
     *
     * @param option
     * @param wrapper  --- 生成二维码的容器
     */
    function makeQArt(option, wrapper) {
        var defaultOption = {
            value: null,  //二维码内容
            imagePath: null,  //img src
            filter: "threshold"  //threshold黑白, color彩色
        };
        option = $.extend(defaultOption, option);
        new QArt(option).make(wrapper);
    }
    function getFileResult(file, callback) {
        if(window.FileReader){
            var files = file.files;
            var reader = new FileReader();
            reader.readAsDataURL(files[0]);
            reader.onload = function (event) {
                if(callback && typeof  callback === "function") {
                    callback(true, event.target.result);
                }
            }
        }else {
            callback(false);
        }
    }
    
    function buildCode(result) {
        var $color = $("#color");
        var $colorChild = $color.find("p");
        var $threshold = $("#threshold");
        var $thresholdChild = $threshold.find("p");
        var value = $("#value").val();
        $("#home p").html(markQrCode({value: value}));
        if(result) {
            makeQArt({value: value, imagePath: result, filter: "color"}, $colorChild[0]);
            makeQArt({value: value, imagePath: result, filter: "threshold"}, $thresholdChild[0]);
        } else{
            $colorChild.html($colorChild.data("content"));
            $thresholdChild.html($thresholdChild.data("content"));
        }
    }
    var result = null;
    $(document).ready(function() {
        buildCode(result);

        $("#value").keyup(function () {
            buildCode(result);
        });

        $(".choose-file").click(function () {
            $("#file").trigger("click");
        });

        $("#file").change(function () {
            getFileResult(this, function (status, result) {
                if(status) {
                    window.result = result;
                    buildCode(result);
                }
            })
        });
    });
</script>

</body>
</html>